//
//  SinaWeibo.m
//  NewsReader
//
//  Created by blacksun on 11-3-1.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SinaWeibo.h"
#import "QOauthKey.h"
#import "QWeiboRequest.h"

@implementation SinaWeibo


-(id)init{
	if (self=[super init]) {
		QOauthKey *temp = [[QOauthKey alloc] init];
		temp.consumerKey = @"4102995404";
		temp.consumerSecret = @"3aeb3b3695614fa6122a562eea356431";
		temp.callbackUrl = @"null";
		temp.tokenKey = [[NSUserDefaults standardUserDefaults] stringForKey:kSina_Token_Key];
		temp.tokenSecret = [[NSUserDefaults standardUserDefaults] stringForKey:kSina_Token_Secret];
        self.oauthKey = temp;
		[temp release];
        
        self.userId = [[NSUserDefaults standardUserDefaults] stringForKey:kSina_User_Id];
        self.userName = [[NSUserDefaults standardUserDefaults] stringForKey:kSina_User_Name];
        
        
		return self;
	}
	return nil;
}


-(void)getRequestToken{
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	
	NSString *responseStr = [request syncRequestWithUrl:kSina_RequestToken_Url httpMethod:@"GET" oauthKey:self.oauthKey parameters:nil files:nil];
	
	[self parseTokenKeyWithResponse:responseStr];//获取request token
	[request release];
}
-(NSURLRequest *)getAuthorizaRequest{
	NSString *urlStr = [[NSString alloc] initWithFormat:@"%@?oauth_token=%@&oauth_callback=oob&display=page",kSina_Authorize_Url,self.oauthKey.tokenKey];
	NSLog(@"%@",urlStr);
	NSURL *url = [[NSURL alloc] initWithString:urlStr];
	[urlStr release];
	NSURLRequest *request = [NSURLRequest requestWithURL:url];
	[url release];
	return request;
}

-(void)getAccessToken{
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSString *retString = [request syncRequestWithUrl:kSina_AccessToken_Url httpMethod:@"GET" oauthKey:oauthKey parameters:nil files:nil];
	[self parseTokenKeyWithResponse:retString];
    self.userId = [self valueForKey:kSina_User_Id_Key ofQuery:retString];
	[request release];
	
}
-(void) getUserName{
    QWeiboRequest *request = [[QWeiboRequest alloc] init];
    NSString *showUserURL = [[NSString alloc] initWithFormat:@"%@/users/show/%@.json",kSina_BaseUrl,self.userId];
    NSString *retString = [request syncRequestWithUrl:showUserURL httpMethod:@"GET" oauthKey:oauthKey parameters:nil files:nil];
	
    NSDictionary *dict = [retString JSONValue];
    
    self.userName = [dict objectForKey:@"name"];
    
    NSLog(@"userName = %@",self.userName);
    
    [request release];
    [showUserURL release];
}

-(void) saveAccessToken{
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	[userDefaults setValue:self.oauthKey.tokenKey forKey:kSina_Token_Key];
	[userDefaults setValue:self.oauthKey.tokenSecret forKey:kSina_Token_Secret];
    [userDefaults setValue:self.userId forKey:kSina_User_Id];
    [userDefaults setValue:self.userName forKey:kSina_User_Name];
    
	[userDefaults synchronize];
}
//发送微薄
-(BOOL) sendTweet:(NSString *)tweet{
	QWeiboRequest *request = [[QWeiboRequest alloc] init];
	NSString *url = [[NSString alloc] initWithFormat:@"%@statuses/update.json",kSina_BaseUrl];
	NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:tweet,@"status",nil];
	[request syncRequestWithUrl:url httpMethod:@"POST" oauthKey:self.oauthKey parameters:parameters files:nil];
	[parameters release];
	[url release];
	return YES;
}

@end
